#ifndef SEQIOT_HISDATA_H
#define SEQIOT_HISDATA_H

#include <time.h>
#include <stdint.h>
#include "model.h"
#include "sched_pub.h"
#include "packet.h"

#define BOF 0x55
#define EOF 0xaa

typedef enum _block_type_e {
    bt_rdb,
    bt_hdb,
    bt_evt,
} block_type_e;

typedef struct _block_head_t {
    uint8_t bof[2]; //头部标记(55h aah)
    uint16_t len; //长度
    uint8_t type; //类型
    uint8_t ver; //版本
    uint8_t enc; //加密算法
    uint8_t _res; //保留
    uint32_t timestamp; //存储时间
} block_head_t;

typedef struct _block_tail_t {
    uint16_t cs; //校验码
    uint8_t eof[2]; //尾部标记(aah 55h)
} block_tail_t;

//数据块的头部和尾部信息
typedef struct _data_block_t {
    block_head_t head;
    block_tail_t tail;
} block_t;

typedef struct _block_params_t {
    __time_t sched_time;
    seqiot_t *seqiot;
} block_params_t;

void block_head_init(block_head_t *self, block_type_e type, time_t timestamp);

void block_tail_init(block_tail_t *self, uint16_t cs);

int block_write(struct _pub_handler_t *handler, device_t *device, segment_e segment);

int block_flush(struct _pub_handler_t *handler);

#endif //SEQIOT_HISDATA_H
